// pages/order_confirm/order_confirm.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
Page({

    /**
     * 页面的初始数据
     */
	data: {
		usewallet: false, //是否使用钱包  true：使用   false：不用
		addressid: '', //默认地址id
	},
	//   、、是否使用钱包
	switch2Change: function (e) {
		var that = this;
		var usewallet = e.detail.value;
		var balance = that.data.balance; //钱包余额
		var goods_price = that.data.goodinfo.goods_price; //商品价格
		var paymoney;//需支付金额
		that.setData({
			usewallet: usewallet
		})
		if (usewallet) {
			// 、、如果使用钱包
			if (goods_price - balance >= 0) {
				paymoney = goods_price - balance; //需支付金额=商品价格-钱包余额
				paymoney = Number(paymoney).toFixed(2);
				that.setData({
					walletpay: balance, //如果商品价格高于余额，那么使用钱包全部金额
					paymoney: paymoney
				})
			} else {
				paymoney = 0; //需支付金额=0
				that.setData({
					walletpay: goods_price, //如果余额高于商品价格，那么需支付金额==0（兑换）
					paymoney: paymoney
				})
			}
		} else {
			//如果不用钱包
			paymoney = goods_price;
			that.setData({
				walletpay: 0, //如果不使用钱包，那么使用钱包的价格==0
				paymoney: paymoney
			})
		}
	},
	//进入的时候判断是否使用钱包的函数
	useWalletOrNot: function () {
		var that = this;
		var usewallet = that.data.usewallet;
		var balance = that.data.balance; //钱包余额
		var goods_price = that.data.goodinfo.goods_price; //商品价格
		var paymoney;//需支付金额
		that.setData({
			usewallet: usewallet
		})
		if (usewallet) {
			// 、、如果使用钱包
			if (goods_price - balance >= 0) {
				paymoney = goods_price - balance; //需支付金额=商品价格-钱包余额
				paymoney = Number(paymoney).toFixed(2);
				that.setData({
					walletpay: balance, //如果商品价格高于余额，那么使用钱包全部金额
					paymoney: paymoney
				})
			} else {
				paymoney = 0; //需支付金额=0
				that.setData({
					walletpay: goods_price, //如果余额高于商品价格，那么需支付金额==0（兑换）
					paymoney: paymoney
				})
			}
		} else {
			//如果不用钱包
			paymoney = goods_price;
			that.setData({
				walletpay: 0, //如果不使用钱包，那么使用钱包的价格==0
				paymoney: paymoney
			})
		}
	},
	//选择地址
	select_address: function (e) {
		//将参数type传过去，用于判断是否可以选择地址
		wx.navigateTo({
			url: '../addresslist/addresslist?type=select',
		})
	},
	//立即支付按钮
	tap_paynow: function (e) {
		var that = this;
		var addressid = that.data.addressid; //以选择的地址id
		var goodid = this.data.id; //商品id
		var orderid = this.data.orderid; //订单id
		var usewallet = that.data.usewallet; //true：使用钱包  false：不使用钱包
		var is_balance = 0;
		if (usewallet) {
			//如果使用钱包
			is_balance = 1;
		}
		if (!addressid) {
			wx.showToast({
				title: '请选择收货地址',
				icon: 'none',
				mask: true,
			})
			return false;
		}
		//请求接口
		var session_str = wx.getStorageSync('session_str');//获取密钥
		wx.showLoading({
			title: '加载中',
			mask: true,
		})
		// 如果是商品下单
		if (goodid) {
			// 、、请求 下单接口
			wx.request({
				url: url + 'Order/createOrder',
				data: {
					session_str: session_str,
					goods_id: goodid,
					address_id: addressid,
					is_balance: is_balance,
					APP_ID: APP_ID,
					sign: md5([
						"session_str" + session_str,
						"goods_id" + goodid,
						"address_id" + addressid,
						"is_balance" + is_balance,
						"APP_ID" + APP_ID,
					])
				},
				header: {},
				method: 'post',
				dataType: 'json',
				responseType: 'text',
				success: function (res) {
					wx.hideLoading();
					if (res.data.code == 200) {
						var result = res.data.data;
						wx.requestPayment({
							timeStamp: result.timeStamp,
							nonceStr: result.nonceStr,
							package: result.package,
							signType: result.signType,
							paySign: result.paySign,
							success: function (res) {
								wx.showToast({
									title: '支付成功！',
									duration: 2000,
									icon: 'success',
									mask: true,
								})
								setTimeout(function (e) {
									wx.redirectTo({
										url: '../orderlist/orderlist',
									})
								}, 2000)
							},
							fail: function (res) {
								wx.hideLoading();
								wx.showToast({
									title: res.data.msg,
									icon: 'none',
									mask: true,
								})
							}
						})
					} else if (res.data.code == 201) {
						wx.showToast({
							title: '支付成功！',
							duration: 2000,
							icon: 'success',
							mask: true,
						})
						setTimeout(function (e) {
							wx.redirectTo({
								url: '../orderlist/orderlist',
							})
						}, 2000)
					} else {
						wx.showToast({
							title: res.data.msg,
							icon: 'none',
							mask: true,
						})
					}
				},
				fail: function (res) {
					wx.hideLoading();
					wx.showToast({
						title: '网络错误',
						icon: 'none',
						mask: true,
					})
				}
			});
		}
		// 如果是订单支付
		if (orderid) {
			wx.request({
				url: url + 'Order/auctionCreateOrder',
				data: {
					session_str: session_str,
					order_id: orderid,
					address_id: addressid,
					is_balance: is_balance,
					APP_ID: APP_ID,
					sign: md5([
						"session_str" + session_str,
						"order_id" + orderid,
						"address_id" + addressid,
						"is_balance" + is_balance,
						"APP_ID" + APP_ID,
					])
				},
				header: {},
				method: 'post',
				dataType: 'json',
				responseType: 'text',
				success: function (res) {
					wx.hideLoading();
					if (res.data.code == 200) {
						var result = res.data.data;
						wx.requestPayment({
							timeStamp: result.timeStamp,
							nonceStr: result.nonceStr,
							package: result.package,
							signType: result.signType,
							paySign: result.paySign,
							success: function (res) {
								wx.showToast({
									title: '支付成功！',
									duration: 2000,
									icon: 'success',
									mask: true,
								})
								setTimeout(function (e) {
									wx.redirectTo({
										url: '../orderlist/orderlist',
									})
								}, 2000)
							},
							fail: function (res) {
								wx.hideLoading();
								wx.showToast({
									title: res.data.msg,
									icon: 'none',
									mask: true,
								})
							}
						})
					} else if (res.data.code == 201) {
						wx.showToast({
							title: '支付成功！',
							icon: 'success',
							duration: 2000,
							mask: true,
						})
						setTimeout(function (e) {
							wx.redirectTo({
								url: '../orderlist/orderlist',
							})
						}, 2000)
					} else {
						wx.showToast({
							title: res.data.msg,
							icon: 'none',
							mask: true,
						})
					}
				},
				fail: function (res) {
					wx.hideLoading();
					wx.showToast({
						title: '网络错误',
						icon: 'none',
						mask: true,
					})
				}
			});
		}

	},

    /**
     * 生命周期函数--监听页面加载
     */
	onLoad: function (options) {
		var id = options.id;//商品id
		var orderid = options.orderid;//订单id
		if (id) {
			this.setData({
				id: id
			})
		}
		if (orderid) {
			this.setData({
				orderid: orderid
			})
		}
		var that = this;
		var id = this.data.id;//商品id
		var orderid = this.data.orderid;//订单id
		var session_str = wx.getStorageSync('session_str'); //获取密钥
		var data = {};//请求接口时穿的参数
		var sonUrl = '';//URL变量
		// 商品
		if (id) {
			data = {
				goods_id: id,
				session_str: session_str,
				APP_ID: APP_ID,
				sign: md5([
					"goods_id" + id,
					"session_str" + session_str,
					"APP_ID" + APP_ID
				])
			}
			sonUrl = 'Order/getOrder';
		}
		// 待支付
		if (orderid) {
			data = {
				order_id: orderid,
				session_str: session_str,
				APP_ID: APP_ID,
				sign: md5([
					"order_id" + orderid,
					"session_str" + session_str,
					"APP_ID" + APP_ID
				])
			}
			sonUrl = 'Order/auctionOrder';
		}
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		var addressid = that.data.addressid; //选择的地址id
		//请求商品详情的接口(或者代付款详情)
		wx.request({
			url: url + sonUrl,
			data: data,
			header: {},
			method: 'post',
			dataType: 'json',
			responseType: 'text',
			success: function (res) {
				wx.hideLoading();
				if (res.data.code == 200) {
					var info_res = res.data.data;
					that.setData({
						goodinfo: info_res.goods_info, //商品信息
						balance: info_res.balance, //余额
						paymoney: info_res.goods_info.goods_price, //支付总额

					})
					that.useWalletOrNot();//调用是否进入页面的时候使用钱包
					if (addressid) {
						//如果已经选择地址   请求地址信息的接口
						wx.request({
							url: url + 'User/addressInfo',
							data: {
								session_str: session_str,
								APP_ID: APP_ID,
								user_address_id: addressid,
								sign: md5([
									"session_str" + session_str,
									"user_address_id" + addressid,
									"APP_ID" + APP_ID,
								])
							},
							header: {},
							method: 'post',
							dataType: 'json',
							responseType: 'text',
							success: function (res) {
								wx.hideLoading();
								if (res.data.code == 200) {
									var result = res.data.data;
									that.setData({
										address: result,
										addressid: result.user_address_id
									})
									console.log('sd' + result)
								} else {
									wx.showToast({
										title: res.data.msg,
										icon: 'none',
										mask: true,
									})
								}

							},
							fail: function (res) {
								wx.hideLoading();
								wx.showToast({
									title: '网络错误',
									icon: 'none',
									mask: true,
								})
							}
						});
					} else {
						//如果未选择地址
						that.setData({
							addressid: info_res.address.user_address_id, //地址id
							address: info_res.address, //地址信息
						})
						console.log(info_res.address)
					}

				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						mask: true,
					})
				}

			},
			fail: function (res) {
				wx.hideLoading();
				wx.showToast({
					title: '网络错误',
					icon: 'none',
					mask: true,
				})
			}
		});

	},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
	onReady: function () {

	},

    /**
     * 生命周期函数--监听页面显示
     */
	onShow: function () {
		
	},

    /**
     * 生命周期函数--监听页面隐藏
     */
	onHide: function () {

	},

    /**
     * 生命周期函数--监听页面卸载
     */
	onUnload: function () {

	},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
	onPullDownRefresh: function () {

	},

    /**
     * 页面上拉触底事件的处理函数
     */
	onReachBottom: function () {

	},
})